<?php
/**
 * User: i@fedax.cn
 * Time: 2018/7/26 14:33
 */

namespace app\admin\model;


class Vehicle extends DataBase
{
    protected $name = 'shop_vehicle';
    protected $autoWriteTimestamp = true;

    public function getUpdateTimeAttr($value)
    {
        return date('Y-m-d', $value);
    }
    /**
     * @package 查询结果
     * @param $order
     * @param $map
     * @param $page_config
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getDataList()
    {
        $page_config = array(
            'page' => input('post.offset', 1),
            'list_rows' => input('post.limit', 100),
        );
        $order = input('post.sort', 'vehicle_id') . " " . input('post.order', 'desc');
        $where = array();//组装搜索语句
        if(!empty(input('start_time')) && !empty(input('end_time'))){
            $start = strtotime(input('start_time')); $end =strtotime(input('end_time'));
            $where[] =['create_time','>=',$start];
            $where[] = ['create_time','<',$end];
        }
        if(!empty(input('key')) && !empty(input('value'))){
            $key = input('key');
            $value= input('value');
            if($key=='type' && $value=='普通'){
                $value = 1;
            }elseif($key=='type' && $value=='专属'){
                $value = 2;
            }
            $where[] = [$key,'=',$value];
        }
        $data = $this
            ->field('vehicle_id,vehicle_name,vehicle_img,vehicle_animation,type,price_15,price_30,is_discount,create_time,is_putaway')
            ->where($where)
            ->order($order)
            ->paginate($page_config);
        return $data;
    }

    public function vehicleUserInfo()
    {
        $map = ['product_id'=>'vehice_id']; //产品id
        $vehicle_id = input('get.vehicle_id');

        //$data = ShopOrder::where(['product_id'=>$vehicle_id,'type'=>1])->field('user_id,create_time,expire_time')->select()->toArray();
        $data = ShopOrder::where(['vehice_id'=>$vehicle_id,'type'=>1])->field('user_id,created_at as create_time,expire_at as expire_time')->select()->toArray();

        foreach($data as $key=>$value){

            $userInfo = User::where('user_id','=',$value['user_id'])->field('u_number as u_id,nickname')->find();
            $data[$key]['u_id'] = $userInfo['u_id'];
            $data[$key]['nickname'] = $userInfo['nickname'];
            $data[$key]['create_time'] = $value['create_time'];
            $data[$key]['expire_time'] =date('Y-m-d H:i:s',$value['expire_time']);
        }
        return $data;
    }

    public function add()
    {
        $newData['vehicle_img'] = oosUpload($_FILES['vehicle_img'],'ss-shopping','oss-cn-beijing.aliyuncs.com');
        $newData['vehicle_animation'] = oosUpload($_FILES['vehicle_animation'],'ss-shopping','oss-cn-beijing.aliyuncs.com');
        if(empty($newData['vehicle_img'])) return ['code'=>-100,'msg'=>'座驾图上传失败'];
        if(empty($newData['vehicle_animation'])) return ['code'=>-100,'msg'=>'特效图上传失败'];
        $newData['vehicle_name'] = input('post.vehicle_name');
        $newData['type'] = input('post.type');
        $newData['price_15'] = input('post.price_15');
        $newData['price_30'] = input('post.price_30');
        $res = $this->save($newData);
        if($res) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];

    }


}